/* 
 * name: Pill.js
 * author: akoyama
 * description: Currently controls color, pill location and boundary collision.
 * 
 */

var PILL_SPAWN_POINT = [45,0];      //This SHOULD change depending on the field location?
var COLORS = [0,1,2];   

function Pill() {
    //These position variables are the center of the square, the origin.
    var gfHeight = gameField.getFieldDimension('height');
    var gfWidth = gameField.getFieldDimension('width');
    var pillWidth = gameField.getPillWidth();
    //subtracting by the width puts it on the left side. 
    //it will never be in the middle .........because there are supposed to be two sides. It's supposed to be even.
    //this.xPos = (gfWidth / 2) - pillWidth ;    //should spawn in the middle. spawns on the second half. not in the middle 
    //this.yPos = gfHeight / 25;    //should spawn in the first 10th of the field. 
    this.xPos = PILL_SPAWN_POINT[0];
    this.yPos = PILL_SPAWN_POINT[1];
    
    var moveIncrement = pillWidth;   //Each increment should be by the width 
    var isPlaced = false;   //used later for collision

    randomizeColor(this);   //sneaky color initializer

    //Moves the pill, checking collision beforehand
    this.move = function(moveDirection){
        if(isPlaced ===false){
            switch(moveDirection){
                case 'left':
                    if(MovingIntoAnything(this, 'left')===false) {
                    //TO-DO: Add the case where a player will go into a hole in a stack of placed pills.
                    //if MovingIntoAnything is false LEFT AND MoveIntoAnything(down) returns true but it's not placed
                    //THEN it's not at the bottom but theres something under it. then it is placed in the 'hole'
                        this.xPos -= moveIncrement;
                    }
                    break;
                case 'right':
                    if(MovingIntoAnything(this, 'right')===false){
                        this.xPos += moveIncrement;
                    }
                    break;
                case 'down':
                    if(MovingIntoAnything(this, 'down')===false) {
                        this.yPos += moveIncrement;
                    }
                    else{
                        //IF the player IS moving into THE BOTTOM BOUNDARY or a 
                        //placed pill and they press down, then the pill is placed.
                        isPlaced = true;
                    }
                    break;
                case 'up':
                    //Here for testing purposes.
                    if(MovingIntoAnything(this, 'up')===false) {
                        this.yPos -= moveIncrement;
                    }
                    else{
                        isPlaced = true;
                    }
                    break;
            }
        }
        else {  //IsPlaced === True
            //If the piece is placed then it is a new PlacedPill. Spawn a new pill.
            var newPlacedPill = new PlacedPill(this.xPos, this.yPos, this.color);
            gameField.addPlacedPill(newPlacedPill);
            gameField.spawnPill();
        }
    };
    
    this.isPlaced = function() {
        var bottomBoundary = gameField.getBoundary('bottom');
        if(this.yPos >= bottomBoundary)
            return true;
        else {
            return false;
        } 
    };
    
    //Access functions. The variables aren't private. Yet.
    this.getPillLocation = function() {
        return [this.xPos, this.yPos];
    };
    
    this.getPillX = function() {
        return this.xPos;
    };
    
    this.getPillY = function() {
        return this.yPos;
    };
    
    this.getPillHeight = function() {
        var pillHeight = this.height;
        return pillHeight;
    };
};

//Sets a random color for the pill
function randomizeColor(self) {
    var colorNumber = COLORS[Math.floor((Math.random() * 3))];
    switch(colorNumber) {
        case 0:
            self.color = 'blue';
            break;
        case 1:
            self.color = 'red';
            break;
        case 2:
            self.color = 'yellow';
            break;
    }    
};